package com.bank.common;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.UUID;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;


public class excelGenerator {

  private WritableCellFormat timesBoldUnderline;
  private WritableCellFormat times;
  private static String inputFile;
  
  public void setOutputFile(String inputFile) {
  this.inputFile = inputFile;
  }

  public void write(List<List<String>> data) throws IOException, WriteException {
	    File file = new File(inputFile);
	    WorkbookSettings wbSettings = new WorkbookSettings();

	    wbSettings.setLocale(new Locale("en", "EN"));

	    WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
	    workbook.createSheet("History", 0);
	    WritableSheet excelSheet = workbook.getSheet(0);
	    createLabel(excelSheet);
	    createContent(excelSheet, data);
	    workbook.write();
	    workbook.close();
	  }
	  private void createLabel(WritableSheet sheet)throws WriteException {
	    WritableFont times12pt = new WritableFont(WritableFont.TIMES, 12);
	    times = new WritableCellFormat(times12pt);
	    times.setWrap(false);

	    WritableFont times10ptBoldUnderline = new WritableFont(WritableFont.TIMES, 14, WritableFont.BOLD, false,UnderlineStyle.SINGLE);
	    timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
	    timesBoldUnderline.setWrap(false);
	    
	    CellView cv = new CellView();
	    cv.setFormat(times);
	    cv.setFormat(timesBoldUnderline);
	    addCaption(sheet, 0, 0, "Transaction id:");
	    addCaption(sheet, 1, 0, "Pay Account:");
	    addCaption(sheet, 2, 0, "Reciever account:");
	    addCaption(sheet, 3, 0, "Amount:");
	    addCaption(sheet, 4, 0, "Transaction time");
	    addCaption(sheet, 5, 0, "Details");
	    for(int x=0;x<6;x++)
	    {
	    	CellView cell = sheet.getColumnView(x);
	        cell.setAutosize(true);
	        sheet.setColumnView(x, cell);
	    }
	  }

	  private void createContent(WritableSheet sheet, List<List<String>> data) throws WriteException,RowsExceededException {
		int i = 1;
		int x = 1;
		Iterator<List<String>> iterator = data.iterator();
	    while (iterator.hasNext()) {
	    	String cc = iterator.next().toString();	
			String cc2 = cc.replace("[", "").replace("]", "");			
			String[] array = cc2.split(",",-1);
			
			addLabel(sheet,0, i, array[1]);
	    	addLabel(sheet,1, i, array[4]);
	    	addLabel(sheet,2, i, array[5]);
	    	addLabel(sheet,3, i, array[7]);
	    	addLabel(sheet,4, i, array[8]);
	    	addLabel(sheet,5, i, array[6]);
	    	CellView cell = sheet.getColumnView(x);
	    	cell.setAutosize(true);
	    	sheet.setColumnView(x, cell);
	    	x++;
	    	i++;
	    }    
	  }

	  private void addCaption(WritableSheet sheet, int column, int row, String s)throws RowsExceededException, WriteException {
		  Label label;
		  label = new Label(column, row, s, timesBoldUnderline);
		  sheet.addCell(label);
	  }
	  private void addLabel(WritableSheet sheet, int column, int row, String s)throws WriteException, RowsExceededException {
		  Label label;
		  label = new Label(column, row, s, times);
		  sheet.addCell(label);
	  }

  public static String createExcel(List<List<String>> data) throws WriteException, IOException {
	UUID uid = UUID.fromString("38400000-8cf0-11bd-b23e-10b96e4ef00d");
	String FileName = uid.randomUUID().toString();
    excelGenerator test = new excelGenerator();
    test.setOutputFile("src\\main\\webapp\\resources\\xls\\excelHistory-"+FileName+".xls");
    test.write(data);
	return "excelHistory-"+FileName+".xls";
  }
} 